home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / util3 / activity.lha / Activity / activity.e < prev    next >
Text File  |  1996-01-01  |  9KB  |  315 lines

  1. -> activity.e - commodity to monitor user activity
  2.  
  3. OPT PREPROCESS
  4.  
  5. MODULE 'icon',
  6.        'commodities',
  7.        'amigalib/argarray',
  8.        'other/ecode',
  9.        'devices/inputevent',
  10.        'dos/dos',
  11.        'exec/libraries',
  12.        'exec/ports',
  13.        'libraries/commodities',
  14.        'intuition/intuition',
  15.        'utility/tagitem',
  16.        'gadtools',
  17.        'libraries/gadtools'
  18.  
  19. ENUM ERR_NONE, ERR_ARGS, ERR_BRKR, ERR_CRCX, ERR_CXERR, ERR_ECODE, ERR_LIB,
  20.      ERR_PORT, ERR_SIG, ERR_MENU
  21.  
  22. CONST EVT_POPUP=1
  23.  
  24. RAISE ERR_BRKR IF CxBroker()=NIL,
  25.       ERR_CRCX IF CreateCxObj()=NIL,
  26.       ERR_LIB  IF OpenLibrary()=NIL,
  27.       ERR_PORT IF CreateMsgPort()=NIL,
  28.       ERR_SIG  IF AllocSignal()=-1
  29.  
  30. DEF broker_mp=NIL:PTR TO mp, broker=NIL, cocustom=NIL, cosignal=NIL,
  31.     task, cxsigflag, signal=-1, cxobjsignal, wnd=NIL:PTR TO window,
  32.     title[30]:STRING,hotkey:PTR TO CHAR,menus=NIL
  33.  
  34. DEF left=0,right=0,move=0,key=0
  35.  
  36. PROC main() HANDLE
  37.   DEF ttypes=NIL, msg, cxfunc
  38.   DEF x,filter
  39.   DEF vis=NIL
  40.   cxbase:=OpenLibrary('commodities.library', 37)
  41.   -> Open the icon.library for support functions, argXXX()
  42.   iconbase:=OpenLibrary('icon.library', 36)
  43.   gadtoolsbase:=OpenLibrary('gadtools.library',36)
  44.   vis:=GetVisualInfoA(OpenWorkBench(),[NIL])
  45.   IF vis=NIL THEN Raise(ERR_MENU)
  46.   menus:=CreateMenusA(
  47.     [ 1,0,'Project',0,0,0,0,
  48.        2,0,'About','A',0,0,0,
  49.        2,0,'Help','?',0,0,0,
  50.        2,0,-1,0,0,0,0,
  51.        2,0,'Reset','R',0,0,0,
  52.        2,0,-1,0,0,0,0,
  53.        2,0,'Hide','H',0,0,0,
  54.        2,0,'Quit','Q',0,0,0,
  55.      0,0,NIL,0,0,0,0]:newmenu,
  56.      [NIL])
  57.   IF menus=NIL THEN Raise(ERR_MENU)
  58.   LayoutMenusA(menus,vis,[NIL])
  59.   broker_mp:=CreateMsgPort()
  60.   cxsigflag:=Shl(1, broker_mp.sigbit)
  61.  
  62.   ttypes:=argArrayInit()
  63.  
  64.   broker:=CxBroker([NB_VERSION, 0, 'Activity',
  65.                     'Activity-meter', 'Show user activity',
  66.                     NBU_UNIQUE OR NBU_NOTIFY, COF_SHOW_HIDE,
  67.                     argInt(ttypes, 'CX_PRIORITY', 0),
  68.                     0, broker_mp, 0]:newbroker, NIL)
  69.  
  70.   IF NIL=(cxfunc:=eCodeCxCustom({cxFunction})) THEN Raise(ERR_ECODE)
  71.   cocustom:=CxCustom(cxfunc, 0)
  72.   AttachCxObj(broker, cocustom)
  73.   signal:=AllocSignal(-1)
  74.   cxobjsignal:=Shl(1, signal)
  75.   cxsigflag:=cxsigflag OR cxobjsignal
  76.   task:=FindTask(NIL)
  77.   cosignal:=CxSignal(task, signal)
  78.   AttachCxObj(cocustom, cosignal)
  79.  
  80.   hotkey:=argString(ttypes, 'CX_POPKEY', 'control alt m')
  81.   filter:=CxFilter(hotkey)
  82.   AttachCxObj(broker, filter)
  83.   x:=CxSender(broker_mp, EVT_POPUP)
  84.   AttachCxObj(filter, x)
  85.   x:=CxTranslate(NIL)
  86.   AttachCxObj(filter, x)
  87.  
  88.   ActivateCxObj(broker, TRUE)
  89.   IF argBool(ttypes,'CX_POPUP',TRUE) THEN appear()
  90.   processMsg()
  91.  
  92. EXCEPT DO
  93.   IF signal<>-1 THEN FreeSignal(signal)
  94.   IF broker THEN DeleteCxObjAll(broker)
  95.   IF ttypes THEN argArrayDone()
  96.   IF broker_mp
  97.     -> Empty the port of all CxMsgs
  98.     WHILE msg:=GetMsg(broker_mp) DO ReplyMsg(msg)
  99.     DeleteMsgPort(broker_mp)
  100.   ENDIF
  101.   IF menus THEN FreeMenus(menus)
  102.   IF vis THEN FreeVisualInfo(vis)
  103.   IF gadtoolsbase THEN CloseLibrary(gadtoolsbase)
  104.   IF iconbase THEN CloseLibrary(iconbase)
  105.   IF cxbase THEN CloseLibrary(cxbase)
  106.   SELECT exception
  107.   CASE ERR_ARGS;  WriteF('Error: Could not parse tooltypes/arguments\n')
  108.   CASE ERR_BRKR;  WriteF('Error: Could not create broker\n')
  109.   CASE ERR_CRCX;  WriteF('Error: Could not create CX object\n')
  110.   CASE ERR_CXERR; WriteF('Error: Could not activate broker\n')
  111.   CASE ERR_ECODE; WriteF('Error: Ran out of memory in eCodeCxCustom()\n')
  112.   CASE ERR_LIB;   WriteF('Error: Could not open commodities.library\n')
  113.   CASE ERR_PORT;  WriteF('Error: Could not create message port\n')
  114.   CASE ERR_SIG;   WriteF('Error: Could not allocate signal\n')
  115.   CASE ERR_MENU;  WriteF('Error: Could not create/layout menus\n')
  116.   ENDSELECT
  117. ENDPROC
  118.  
  119. PROC processMsg()
  120.   DEF msg:PTR TO intuimessage, sigrcvd, msgid, msgtype, done=FALSE, class, code
  121.   DEF menunumber,item:PTR TO menuitem,command
  122.   REPEAT
  123.     IF wnd=NIL
  124.       sigrcvd:=Wait(SIGBREAKF_CTRL_C OR cxsigflag)
  125.     ELSE
  126.       sigrcvd:=Wait(SIGBREAKF_CTRL_C OR cxsigflag OR Shl(1,wnd.userport.sigbit))
  127.       WHILE msg:=GetMsg(wnd.userport)
  128.         class:=msg.class
  129.         code:=msg.code
  130.         ReplyMsg(msg)
  131.         SELECT class
  132.           CASE IDCMP_CLOSEWINDOW
  133.             disappear()
  134.           CASE IDCMP_VANILLAKEY
  135.             SELECT code
  136.               CASE 27
  137.                 disappear()
  138.               CASE "Q"
  139.                 done:=TRUE
  140.               CASE "q"
  141.                 disappear()
  142.               CASE "x"
  143.                 done:=TRUE
  144.               CASE "X"
  145.                 done:=TRUE
  146.               CASE "h"
  147.                 disappear()
  148.               CASE "H"
  149.                 disappear()
  150.               CASE "a"
  151.                 about()
  152.               CASE "A"
  153.                 about()
  154.               CASE "r"
  155.                 left:=0;right:=0;move:=0;key:=0
  156.                 StringF(title,'L:\d R:\d M:\d K:\d',left,right,move,key)
  157.                 SetWindowTitles(wnd,title,-1)
  158.               CASE "R"
  159.                 left:=0;right:=0;move:=0;key:=0
  160.                 StringF(title,'L:\d R:\d M:\d K:\d',left,right,move,key)
  161.                 SetWindowTitles(wnd,title,-1)
  162.             ENDSELECT
  163.           CASE IDCMP_RAWKEY
  164.             SELECT code
  165.               CASE 95
  166.                 help()
  167.             ENDSELECT
  168.           CASE IDCMP_MENUPICK
  169.             menunumber:=code AND $FFFF
  170.             WHILE (menunumber<>MENUNULL)
  171.               item:=ItemAddress(menus,menunumber)
  172.               command:=item.command
  173.               SELECT command
  174.                 CASE "A"
  175.                   about()
  176.                 CASE "?"
  177.                   help()
  178.                 CASE "R"
  179.                   left:=0;right:=0;move:=0;key:=0
  180.                   StringF(title,'L:\d R:\d M:\d K:\d',left,right,move,key)
  181.                   SetWindowTitles(wnd,title,-1)
  182.                 CASE "H"
  183.                   disappear()
  184.                 CASE "Q"
  185.                   done:=TRUE
  186.               ENDSELECT
  187.               menunumber:=item.nextselect AND $FFFF
  188.               EXIT done
  189.               EXIT wnd=NIL
  190.             ENDWHILE
  191.         ENDSELECT
  192.         EXIT wnd=NIL
  193.       ENDWHILE
  194.     ENDIF
  195.  
  196.     WHILE msg:=GetMsg(broker_mp)
  197.       msgid:=CxMsgID(msg)
  198.       msgtype:=CxMsgType(msg)
  199.       ReplyMsg(msg)
  200.  
  201.       SELECT msgtype
  202.         CASE CXM_COMMAND
  203.           SELECT msgid
  204.             CASE CXCMD_DISABLE
  205.               ActivateCxObj(broker, FALSE)
  206.             CASE CXCMD_ENABLE
  207.               ActivateCxObj(broker, TRUE)
  208.             CASE CXCMD_APPEAR
  209.               appear()
  210.             CASE CXCMD_DISAPPEAR
  211.               disappear()
  212.             CASE CXCMD_KILL
  213.               done:=TRUE
  214.             CASE CXCMD_UNIQUE
  215.               appear()
  216.           ENDSELECT
  217.         CASE CXM_IEVENT
  218.           appear()
  219.       ENDSELECT
  220.     ENDWHILE
  221.  
  222.     IF sigrcvd AND SIGBREAKF_CTRL_C THEN done:=TRUE
  223.  
  224.     -> Check to see if the signal CxObj signalled us.
  225.     IF sigrcvd AND cxobjsignal
  226.       StringF(title,'L:\d R:\d M:\d K:\d',left,right,move,key)
  227.       IF wnd
  228.         SetWindowTitles(wnd,title,-1)
  229.       ENDIF
  230.     ENDIF
  231.   UNTIL done
  232.   disappear()
  233. ENDPROC
  234.  
  235. PROC appear()
  236.   IF wnd=NIL
  237.     wnd:=OpenWindowTagList(NIL,
  238.     [WA_TITLE,title,WA_SCREENTITLE,'Activity by Jilles Tjoelker',
  239.      WA_WIDTH,300,WA_HEIGHT,11,WA_LEFT,0,WA_TOP,0,
  240.      WA_CLOSEGADGET,TRUE,WA_DEPTHGADGET,TRUE,WA_DRAGBAR,TRUE,
  241.      WA_IDCMP,IDCMP_CLOSEWINDOW OR IDCMP_VANILLAKEY OR IDCMP_RAWKEY OR IDCMP_MENUPICK,
  242.      WA_ACTIVATE,FALSE,
  243.      TAG_DONE])
  244.      SetMenuStrip(wnd,menus)
  245.   ELSE
  246.     WindowToFront(wnd)
  247.   ENDIF
  248.   StringF(title,'L:\d R:\d M:\d K:\d',left,right,move,key)
  249.   SetWindowTitles(wnd,title,-1)
  250. ENDPROC
  251.  
  252. PROC disappear()
  253.   IF wnd
  254.     ClearMenuStrip(wnd)
  255.     CloseWindow(wnd)
  256.     wnd:=NIL
  257.   ENDIF
  258. ENDPROC
  259.  
  260. PROC cxFunction(cxm, co)
  261.   DEF ie:PTR TO inputevent
  262.  
  263.   ie:=CxMsgData(cxm)
  264.  
  265.   IF ie.class=IECLASS_RAWMOUSE
  266.     IF ie.code=IECODE_LBUTTON
  267.       left++
  268.       DivertCxMsg(cxm, co, co)
  269.     ENDIF
  270.     IF ie.code=IECODE_RBUTTON
  271.       right++
  272.       DivertCxMsg(cxm, co, co)
  273.     ENDIF
  274.     IF ie.code=IECODE_NOBUTTON
  275.       move++
  276.       DivertCxMsg(cxm, co, co)
  277.     ENDIF
  278.   ENDIF
  279.   IF ie.class=IECLASS_RAWKEY
  280.     IF ie.code<=95
  281.       key++
  282.       DivertCxMsg(cxm, co, co)
  283.     ENDIF
  284.   ENDIF
  285. ENDPROC
  286.  
  287. PROC vers() IS '$VER: Activity 1.0 (31-dec-1995)'
  288.  
  289. PROC about()
  290.   EasyRequestArgs(0,
  291.     [20,0,'About Activity',
  292.     'Activity by Jilles Tjoelker\n\nMy address is:\nJ. Tjoelker\nCraterlaan 6\n5632 AG Eindhoven\nTHE NETHERLANDS\n\nHotkey: \s\n\s',
  293.     'OK']:easystruct,
  294.     0,[hotkey,vers()])
  295. ENDPROC
  296.  
  297. PROC help()
  298.   EasyRequestArgs(0,
  299.     [20,0,'Activity help',
  300.     'Function: Monitors user activity.\n\nL: Left mouse button \nR: Right mouse button\nM: Mouse movement\nK: Keyboard \n\nKeys:\n=====\nESC, q, h, H - Hide window\nQ, x, X      - Quit activity\nR, r         - Reset counters\nA, a         - About this program\nHELP         - This requester\n\n\s - Popup',
  301.     'OK']:easystruct,
  302.     0,[hotkey])
  303. ENDPROC
  304.  
  305. PROC argBool(ttypes,name,val)
  306.   DEF s:PTR TO CHAR
  307.   s:=argString(ttypes,name,NIL)
  308.   IF s
  309.     val:=TRUE
  310.     IF (s[0]="n") OR (s[0]="N")
  311.       val:=FALSE
  312.     ENDIF
  313.   ENDIF
  314. ENDPROC val
  315.